<?php 
	include("../includes/connect_db.php");
	$idresto_menu = (isset($_POST["idresto_menu"])) ? $_POST["idresto_menu"] : "" ;
	$bill = (isset($_POST["bill"])) ? $_POST["bill"] : "" ;

	$sql_resto_menu = "SELECT * FROM resto_menu WHERE id = '$idresto_menu'";
	$query_resto_menu = mysql_query($sql_resto_menu);
	$resto_menu = mysql_fetch_array($query_resto_menu);

	if ($resto_menu['jumlah'] == 1) {
		$sql_update = "UPDATE resto_menu SET bill = '2' WHERE id = '$idresto_menu'";
		$query_update = mysql_query($sql_update);
	} else {
		$jumlah_upd = $resto_menu['jumlah'] - 1;
		$sql_update = "UPDATE resto_menu SET jumlah = '$jumlah_upd' WHERE id = '$idresto_menu'";
		$query_update = mysql_query($sql_update);

		$sql_check = "SELECT * FROM resto_menu WHERE idresto = '$resto_menu[idresto]' AND idmenu = '$resto_menu[idmenu]' AND bill = '$bill'";
		$query_check = mysql_query($sql_check);
		if (mysql_num_rows($query_check) == 0) {
			$sql_insert = "INSERT INTO resto_menu (idresto, idmenu, jumlah, bill) VALUES ('$resto_menu[idresto]', '$resto_menu[idmenu]', '1', '$bill')";
			$query_insert = mysql_query($sql_insert);
		} else {
			$query_check = mysql_query($sql_check);
			$check = mysql_fetch_array($query_check);
			$jumlah_upd = $check['jumlah'] + 1;
			$sql_update = "UPDATE resto_menu SET jumlah = '$jumlah_upd' WHERE idresto_menu = '$check[id]'";
			$query_update = mysql_query($sql_update);
		}
	}
?>

<?php
	$sql = "SELECT * FROM resto_menu WHERE idresto = '$resto_menu[idresto]' AND bill = '$bill'";
	$query = mysql_query($sql);
	while ($result = mysql_fetch_array($query)) {
		$sql_menu = "SELECT * FROM menu WHERE id = $result[idmenu]";
		$query_menu = mysql_query($sql_menu);
		$menu = mysql_fetch_array($query_menu);
?>
		<tr class="new-resto_menu">
			<td></td>
			<td>
				<?php echo $menu['nama']; ?>
			</td>
			<td>
				<?php echo $result['jumlah'] ?>
				<input type="hidden" value="<?php echo $result['jumlah'] ?>" class="new-jumlah">
			</td>
			<td>
				<input type="text" name="" class="new-harga currency" value="<?php echo $menu['harga']; ?>" style="width: auto;">
				<SCRIPT TYPE="text/javascript">$('.currency').autoNumeric('init');</SCRIPT>
			</td>
			<td class="new-total"><?php echo $menu_harga * $result['jumlah']; ?></td>
		</tr>
<?php
	}
?>